package com.baizhi.action;

import com.baizhi.entity.Food;
import com.baizhi.service.FoodService;
import com.baizhi.service.impl.FoodServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FootAction {
    private FoodService fs = new FoodServiceImpl();
    private Map<String,Object> map = new HashMap<>();
    private ObjectMapper mapper = new ObjectMapper();
    private HttpServletResponse response = ServletActionContext.getResponse();
    private PrintWriter pw;



    private Integer pageNum = 1;
    private Integer pageSize =3;
    private Integer id;




    public void show(){

        response.setCharacterEncoding("UTF-8");
        String jsonMap = null;

        try {
            pw = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            List<Food> foods = fs.showFoods(pageNum, pageSize);
            PageInfo<Food> pageInfo = new PageInfo<>(foods);
            int pages = pageInfo.getPages();
            map.put("status","success");
            map.put("foods",foods);
            map.put("pages",pages);
        } catch (Exception e) {
            map.put("status","failed");
            throw new RuntimeException(e);
        }finally{
            try {
                jsonMap = mapper.writeValueAsString(map);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }


        pw.print(jsonMap);
        pw.flush();
    }

    public void remove(){
        response.setCharacterEncoding("UTF-8");
        String jsonMap = null;

        try {
            try {
                pw = response.getWriter();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            fs.removeFoodById(id);
            map.put("status","success");
        } catch (Exception e) {
            map.put("status","failed");
            throw new RuntimeException(e);
        }finally{
            try {
                jsonMap = mapper.writeValueAsString(map);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }

        pw.print(jsonMap);
        pw.flush();

    }





    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}
